package ch.sfug.data {

	/**
	 * a simple iterator, that steps over all elements of an xmllist
	 * @author marcel 
	 * adopted loops aray iterator for xml lists
	 */

	public class XmlListIterator {
		private var _SelectedItem:Number;
		private var _List:XMLList;		

		public function XmlListIterator( xml:XMLList = null) {
			reset();
			if( xml == null ) _List = xml;
		}
				public function hasNext():Boolean {		
			if( isNaN(_SelectedItem) ) {
				// start case
				return _List.length() > 0;
			} 
            else {
				return _SelectedItem < _List.length() - 1;
			}
		}

		
		public function hasPrevious():Boolean {
			if( isNaN(_SelectedItem) ) {
				// start case
				return _List.length() > 0;
			}
            else {
				return _SelectedItem - 1 >= 0;
			}
		}

		
		public function next(  ):Object {
			if( hasNext() ) {
				_SelectedItem = ( isNaN(_SelectedItem) ) ? 0 : _SelectedItem + 1;
				return _List[ _SelectedItem];
			} 
            else {
				return null;
			}
		}

		
		public function previous(  ):Object {
			if( hasPrevious() ) {
				_SelectedItem = ( isNaN(_SelectedItem) ) ? ( _List.length() - 1 ) : _SelectedItem - 1;
				return _List[_SelectedItem];
			} 
            else {
				return null;
			}
		}

		
		public function reset(  ):void {
			_SelectedItem = NaN;
		}

		
		public function current(  ):Object {
			return ( _List != null && _List.length() > 0 ) ? [_SelectedItem] : null;
		}

		
		public function getCurrentValueItem( val:String ):Object {
			return _List[_SelectedItem][val];
		}

		
		public function getCurrentListItem():Object {
			return _List[_SelectedItem];
		}

		
		public function set currentSelection( val:Number ):void {
			_SelectedItem = val;
		}

		
		public function get currentSelection():Number {
			return _SelectedItem;
		}

		
		public function set data( xml:XMLList ):void {
			_List = xml;
		}
	}
}